package com.apexedu.portal.controller;

import com.apexedu.biz.Const;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.IdConstants;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.constant.ActionConstant;
import com.apexedu.portal.entity.TLink;
import com.apexedu.portal.util.LittleTimer;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import com.apexedu.util.json.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 友情链接
 * Created by ly on 2014/8/8.
 */
@Controller
public class LinkController extends BaseController {

    @Resource
    private BaseDao baseDao;

    /**
     * 初始界面
     *
     * @return 初始界面
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_init, method = RequestMethod.GET)
    public String init() {
        debug("<<{}>> init start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            return ActionConstant.Link.Forward_init;
        } catch (Exception e) {
            error("<<{}>> init error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> init end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 树结构
     *
     * @return 初始界面
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_tree, method = RequestMethod.GET)
    public String tree() {
        debug("<<{}>> tree start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            return ActionConstant.Link.Forward_tree;
        } catch (Exception e) {
            error("<<{}>> tree error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> tree end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 获取信息列表集合
     *
     * @return 信息列表集合
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_getListXx, method = RequestMethod.GET)
    @ResponseBody
    public String getListXx() {
        debug("<<{}>> getListXx start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            List<TLink> list = DBUtil.queryAllBeanList("select * from t_link where 1=1 order by orderno", TLink.class);
            String jsonStr = JsonUtil.getJSONString(list);
            debug("<<{}>> getListXx : jsonstr : {}", this.getClass().getSimpleName(), jsonStr);
            return jsonStr;
        } catch (Exception e) {
            error("<<{}>> getListXx error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getListXx end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 维护友情链接信息界面
     *
     * @param request --
     * @param map     --
     * @return 列表信息界面
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_edit, method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap map) {
        debug("<<{}>> edit start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            String wid = request.getParameter("wid");
            TLink link;
            if (StringUtil.isNotBlank(wid)) {
                debug("<<{}>> edit : wid : {}", this.getClass().getSimpleName(), wid);
                link = DBUtil.queryBean("select * from t_link where wid = ?", TLink.class, wid);
            } else {
                String superid = request.getParameter("superid");
                TLink superLink = DBUtil.queryBean("select * from t_link where wid = ?", TLink.class, superid);
                link = new TLink();
                link.setSuperid(superid);
                link.setSupername(superLink.getName());
                link.setType(superLink.getType());
                link.setLev(superLink.getLev() + 1);
            }
            map.put("link", link);
            return ActionConstant.Link.Forward_edit;
        } catch (Exception e) {
            error("<<{}>> edit error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> edit end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 保存友情链接信息
     *
     * @param link 友情链接信息
     * @param map  --
     * @return 友情链接信息维护界面
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_edit, method = RequestMethod.POST)
    public String save(TLink link, ModelMap map,HttpServletRequest request) {
        debug("<<{}>> save start", this.getClass().getSimpleName());
        TSysUser tSysUser = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        LittleTimer timer = new LittleTimer();
        try {
            if (StringUtil.isNotBlank(link.getWid())) {
                baseDao.updateNotNull(link);
                BusinessLogUtil.log(tSysUser.getUserloginid(), Const.CZDX_T_LINK, Const.LOG_ACTION_UPDATE, IdLoginAction.getIpAddr(request));
            } else {
                link.setWid(SeqFactory.getNewSequenceAlone());
                baseDao.save(link);
                BusinessLogUtil.log(tSysUser.getUserloginid(), Const.CZDX_T_LINK, Const.LOG_ACTION_SAVE, IdLoginAction.getIpAddr(request));

            }

            debug("<<?>> save , link : {}", this.getClass().getName(), link.toString());
            map.put("save", true);
//            lbxx.setUpname((String) DBUtil.queryFieldValue("select name from t_lbxx where wid=?", lbxx.getUpid()));
            map.put("link", link);

            return ActionConstant.Link.Forward_edit;
        } catch (Exception e) {
            error("<<{}>> save error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> save end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 删除友情链接
     *
     * @param id 链接id
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_delLink, method = RequestMethod.DELETE)
    @ResponseBody
    public void delLink(@PathVariable(ActionConstant.Variable_Id) String id,HttpServletRequest request) {
        debug("<<{}>> delLink start", this.getClass().getSimpleName());
        TSysUser tSysUser = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
        LittleTimer timer = new LittleTimer();
        try {
            DBUtil.executeSQL("delete from t_link where wid=?", id);
            BusinessLogUtil.log(tSysUser.getUserloginid(), Const.CZDX_T_LINK, Const.LOG_ACTION_DEL, IdLoginAction.getIpAddr(request),"删除wid:"+id);
            DBUtil.executeSQL("delete from t_link where superid=?", id);
            BusinessLogUtil.log(tSysUser.getUserloginid(), Const.CZDX_T_LINK, Const.LOG_ACTION_DEL, IdLoginAction.getIpAddr(request),"删除superid:"+id);
        } catch (Exception e) {
            error("<<{}>> delLink error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> delLink end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 获取友情链接
     *
     * @return 友情链接集合json字符串
     */
    @RequestMapping(value = ActionConstant.Link.RequestMapping_getLinks, method = RequestMethod.GET)
    @ResponseBody
    public String getLinks() {
        debug("<<{}>> save start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            String sql = "select * from (select t.name,t.type,t.lev,case when t.lev=1 then orderno*100000000 else " +
                    "(select orderno from t_link t1 where t.superid=t1.wid)*100000000+orderno end as orderno,link from T_LINK t" +
                    " where t.lev in (1,2) and state=1 order by lev,superid,orderno) order by orderno";
            List<TLink> list = DBUtil.queryAllBeanList(sql, TLink.class);
            return JsonUtil.getJSONString(list);
        } catch (Exception e) {
            error("<<{}>> save error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> save end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }
}
